package menuListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;

import graph.PaintPanel;
import model.GameModel;
import tool.ReadConfig;

public class LevelMenuItemListener implements ActionListener {
	private PaintPanel paint;
	private GameModel gameModel;
	private JMenu level;
	private ReadConfig readConfig;
	public LevelMenuItemListener(PaintPanel paint,JMenu level,GameModel gameModel,ReadConfig readConfig) {
		this.paint=paint;
		this.level=level;
		this.gameModel=gameModel;
		this.readConfig=readConfig;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		int result = JOptionPane.showConfirmDialog(null, "更换难易度必须洗牌，是否继续？");
		String level2String[]={"易","中","难"};
		JRadioButtonMenuItem menuItem=(JRadioButtonMenuItem) e.getSource();
		if(result!=0){//如果没有选是
			for(int i=0;i<level.getItemCount();i++){
				JRadioButtonMenuItem item=(JRadioButtonMenuItem) level.getItem(i);
				if(item.getText().equals(level2String[gameModel.getLevel()]))
					item.setSelected(true);
			}
			return ;
		}
		int level=0;
		switch(menuItem.getText()){
		case "难":
			level=2;
			break;
		case "中":
			level=1;
			break;
		case "易":
			level=0;
			break;
		}
		gameModel.setLevel(level);
		readConfig.setDefaultLevel(level);
		gameModel.refreshPokers();
		paint.repaint();
		menuItem.setSelected(true);
	}

}
